﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Caliburn.Micro;
using FuelTracker.Services;
using FuelTracker.Models;
using FuelTracker.Resources;
using System.Diagnostics;
using FuelTracker.Framework;
using System.Globalization;

namespace FuelTracker.ViewModels
{
    public class CreateFuelLogViewModel : PropertyChangedBase
    {
        private readonly INavigationService navigationService;
        private readonly IStorageService storageService;
        private readonly ParameterService parameterService;

        private DateTime date;
        public DateTime Date
        {
            get { return date; }
            set
            {
                if (date != value)
                {
                    date = value;
                }
            }
        }

        private String distanceDriven;
        public String DistanceDriven
        {
            get { return distanceDriven; }
            set
            {
                distanceDriven = value;
                NotifyOfPropertyChange(() => CanSave);
            }
        }

        private String fuelAmount;
        public String FuelAmount
        {
            get { return fuelAmount; }
            set
            {
                fuelAmount = value;
                NotifyOfPropertyChange(() => CanSave);
            }
        }

        private String totalPrice;
        public String TotalPrice
        {
            get { return totalPrice; }
            set
            {
                totalPrice = value;
                NotifyOfPropertyChange(() => CanSave);
            }
        }

                
        public Vehicle Vehicle { get; set; }

        #region Localization
        public string LabelApplicationTitleCaps
        {
            get { return Texts.Lbl_ApplicationTitleAllCaps; }
        }

        public string LabelNewFuelLog 
        {
            get { return Texts.Lbl_NewFuelLog; }
        }

        public string LabelDate
        {
            get { return Texts.Lbl_Date; }
        }

        public string LabelDistanceDriven
        {
            get { return Texts.Lbl_DistanceDriven; }
        }

        public string LabelFuelAmount
        {
            get { return Texts.Lbl_FuelAmount; }
        }

        public string LabelTotalPrice
        {
            get { return Texts.Lbl_TotalPrice; }
        }

        public string LabelCancel
        {
            get { return Texts.Lbl_Cancel; }
        }

        public string LabelSave
        {
            get { return Texts.Lbl_Save; }
        }

        #endregion

        public CreateFuelLogViewModel(INavigationService navigationService, IStorageService storageService, ParameterService parameterService)
        {
            this.storageService = storageService;
            this.navigationService = navigationService;
            this.parameterService = parameterService;

            this.Vehicle = (Vehicle) parameterService.Pop();
            date = DateTime.Today;
        }

        private decimal GetValidDecimal(string value)
        {
            if (String.IsNullOrEmpty(value))
                return 0;

            string result = "";
            Char decimalSeparator =  CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator[0];
            Char groupSeparator = CultureInfo.CurrentUICulture.NumberFormat.NumberGroupSeparator[0];

            foreach (Char c in value)
            {
                if (Char.IsDigit(c) || c == decimalSeparator || c == groupSeparator)
                    result += c;
            }

            decimal outValue;
            if (!Decimal.TryParse(result, out outValue))
                return 0;

            return outValue;
        }

   
        public void Save()
        {
            DistanceDrivenLostFocus();
            FuelAmountLostFocus();

            TotalPriceLostFocus();
            if (!CanSave)
                return;

            try
            {
                FuelLog fuelLog = new FuelLog(
                    date: date,
                    distanceDriven: Decimal.Parse(distanceDriven),
                    fuelAmount: Decimal.Parse(fuelAmount),
                    totalPrice: Decimal.Parse(totalPrice)
                    );

                Vehicle.FuelLogs.Add(fuelLog);
                storageService.SaveVehicle(Vehicle);

                parameterService.Push(fuelLog);
                navigationService.GoBack();
            }
            catch
            {
                //TODO: uuuiiiiii
            }
        }


        public bool CanSave
        {
            get
            {
                return !String.IsNullOrEmpty(distanceDriven) &&
                    !String.IsNullOrEmpty(fuelAmount) &&
                    !String.IsNullOrEmpty(totalPrice);
            }
        }
                
        public void DistanceDrivenLostFocus()
        {
            decimal d = GetValidDecimal(distanceDriven);
            distanceDriven = d.ToString("###,###.####");
            NotifyOfPropertyChange(() => DistanceDriven);
        }

        public void FuelAmountLostFocus()
        {
            decimal d = GetValidDecimal(fuelAmount);
            fuelAmount = d.ToString("###,###.####");
            NotifyOfPropertyChange(() => FuelAmount);
        }
        
        public void TotalPriceLostFocus()
        {
            decimal d = GetValidDecimal(totalPrice);
            totalPrice = d.ToString("###,###.####");
            NotifyOfPropertyChange(() => TotalPrice);
        }

    }
}
